package com.xnyzc.lhy.mis.service.impl.message;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xnyzc.lhy.common.entity.Rv;
import com.xnyzc.lhy.common.exception.PangException;
import com.xnyzc.lhy.common.penum.EErrorCode;
import com.xnyzc.lhy.common.util.CheckUtil;
import com.xnyzc.lhy.common.util.DateUtil;
import com.xnyzc.lhy.common.util.IDUtil;
import com.xnyzc.lhy.common.util.TokenUtil;
import com.xnyzc.lhy.mis.entity.account.OaDAccountAdjustment;
import com.xnyzc.lhy.mis.entity.dictionaries.OaSysDict;
import com.xnyzc.lhy.mis.entity.message.OaSysMessageDriver;
import com.xnyzc.lhy.mis.entity.message.OaSysMessageInapp;
import com.xnyzc.lhy.mis.entity.message.OaSysMessageInappInfo;
import com.xnyzc.lhy.mis.entity.param.message.OaSysMessageInappParam;
import com.xnyzc.lhy.mis.mapper.dictionaries.OaSysDictMapper;
import com.xnyzc.lhy.mis.mapper.message.OaSysMessageDriverMapper;
import com.xnyzc.lhy.mis.mapper.message.OaSysMessageInappMapper;
import com.xnyzc.lhy.mis.mapper.user.OaDUserMapper;
import com.xnyzc.lhy.mis.service.message.IOaSysMessageDriverService;
import com.xnyzc.lhy.mis.service.message.IOaSysMessageInappService;
import com.xnyzc.lhy.mis.entity.user.OaDUser;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.poi.poifs.filesystem.ODocumentInputStream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * <p>
 * 后台管理 应用内消息表 服务实现类
 * </p>
 *
 * @author wangshuo
 * @since 2019-10-20
 */
@Service
public class OaSysMessageInappServiceImpl extends ServiceImpl<OaSysMessageInappMapper, OaSysMessageInapp> implements IOaSysMessageInappService {
    @Autowired
    OaSysMessageInappMapper oaSysMessageInappMapper;
    @Autowired
    OaSysMessageDriverMapper oaSysMessageDriverMapper;
    @Autowired
    IOaSysMessageDriverService oaSysMessageDriverService;
    @Autowired
    OaSysDictMapper oaSysDictMapper;
    @Autowired
    private OaDUserMapper oaDUserMapper;

    @Override
    @Transactional
    public Rv insertSysMessageInapp(OaSysMessageInappParam oaSysMessageInappParam) {
        if(oaSysMessageInappParam != null && oaSysMessageInappParam.getOaSysMessageInapp()!= null){
            OaSysMessageInapp oaSysMessageInapp = oaSysMessageInappParam.getOaSysMessageInapp();
            if(!oaSysMessageInapp.isAllDriver() && oaSysMessageInappParam.getDriverIds().size() == 0)
            {
                return Rv.wrap(EErrorCode.noData);
            }
            Long id = IDUtil.nextId();
            Date date = new Date();
            Long sysUserId = TokenUtil.getTokenUserId();
            oaSysMessageInapp.setInappId(id);
            oaSysMessageInapp.setCreateTime(date);
            oaSysMessageInapp.setCreateUser(sysUserId);
            oaSysMessageInappMapper.insert(oaSysMessageInapp);
            OaSysMessageDriver oaSysMessageDriver = null;
            List<OaSysMessageDriver> oaSysMessageDriverList = new ArrayList<>();
            if(oaSysMessageInapp.isAllDriver())
            {
                List<OaDUser> driverList = oaDUserMapper.selectByMap(null);
                for (OaDUser driver : driverList)
                {
                    Long messageDriverId = IDUtil.nextId();
                    oaSysMessageDriver = new OaSysMessageDriver();
                    oaSysMessageDriver.setMessageDriverId(messageDriverId);
                    oaSysMessageDriver.setDriverId(driver.getDriverId());
                    oaSysMessageDriver.setMessageType(0);//消息类型=0 应用内消息
                    oaSysMessageDriver.setInAppId(oaSysMessageInapp.getInappId());
                    oaSysMessageDriver.setCreateTime(date);

                    oaSysMessageDriverList.add(oaSysMessageDriver);
                }
            }
            else
            {
                List<Long> longList = oaSysMessageInappParam.getDriverIds();
                for (Long aLong : longList)
                {
                    Long messageDriverId = IDUtil.nextId();
                    oaSysMessageDriver = new OaSysMessageDriver();
                    oaSysMessageDriver.setMessageDriverId(messageDriverId);
                    oaSysMessageDriver.setDriverId(aLong);
                    oaSysMessageDriver.setMessageType(0); //消息类型=0 应用内消息
                    oaSysMessageDriver.setInAppId(oaSysMessageInapp.getInappId());
                    oaSysMessageDriver.setCreateTime(date);

                    oaSysMessageDriverList.add(oaSysMessageDriver);
                    // 修改为批量新增
                    // oaSysMessageDriverMapper.insert(oaSysMessageDriver);
                }
            }
            if(oaSysMessageDriverList.size() > 0)
            {
                oaSysMessageDriverService.saveBatch(oaSysMessageDriverList);
            }
        }else{
            return Rv.wrap(EErrorCode.noData);
        }
        return Rv.wrap(EErrorCode.success);
    }

    @Override
    public Rv querySysMessageInappList(Long current,Long pageSize) {
        IPage iPage = new Page();
        iPage.setSize(pageSize);
        iPage.setCurrent(current);
        List<OaSysMessageInappInfo> oaSysMessageInapps = oaSysMessageInappMapper.querySysMessageInappList(iPage);
        Date date = new Date();
        for (OaSysMessageInappInfo oaSysMessageInapp : oaSysMessageInapps) {
            //生效时间与失效时间比较
            if(date.compareTo(oaSysMessageInapp.getEffectTimeConversion())<0){
                oaSysMessageInapp.setMessageStatus("未生效");
            }else if(date.compareTo(oaSysMessageInapp.getFailureTimeConversion())>0){
                oaSysMessageInapp.setMessageStatus("已失效");
            }else{
                oaSysMessageInapp.setMessageStatus("生效中");
            }

            oaSysMessageInapp.setEffectTime(DateFormatUtils.format(oaSysMessageInapp.getEffectTimeConversion(),"yyyy-MM-dd HH:mm:ss"));
            oaSysMessageInapp.setFailureTime(DateFormatUtils.format(oaSysMessageInapp.getFailureTimeConversion(), "yyyy-MM-dd HH:mm:ss"));
        }
        iPage.setRecords(oaSysMessageInapps);
        return Rv.wrap(EErrorCode.success,iPage);
    }

    @Override
    public Rv querySysMessageType() {
        Long sysUserId=TokenUtil.getTokenUserId();
        List<String> dataScope=TokenUtil.getRoleDataScope();
        if(CheckUtil.objIsEmpty(dataScope)||CheckUtil.objIsEmpty(sysUserId)){
            throw PangException.create(EErrorCode.missingArg);
        }
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq(OaSysDict.TYPE, OaSysMessageInapp.MESSAGE_STATUS);
        List<OaSysDict> list = oaSysDictMapper.selectList(queryWrapper);
        return Rv.wrap(list);
    }
}
